package youleche.com.controler.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BPagelimit;
import youleche.com.caches.CacheFactory;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.model.MPagelimit;
import youleche.com.util.DateUtil;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SPagelimitServlet
 */
public class SPagelimitServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SPagelimitServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * dopost
     */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//添加权限信息
				this.addPagelimit(request, response);
				break;
			case 1:
				//获取所有权限信息
				this.getAllPageNodes(request, response);
				break;
			case 2:
				////根据id获取节点
				this.getNodesById(request, response);
				break;
		}
	}
	
	/**
	 * 根据id获取节点
	 * @param request
	 * @param response
	 */
	private void getNodesById(HttpServletRequest request, HttpServletResponse response){
		
	}
	
	/**
	 * 获取所有节点
	 * @param request
	 * @param response
	 */
	private void getAllPageNodes(HttpServletRequest request, HttpServletResponse response){
		try {
			@SuppressWarnings({ "unchecked", "static-access" })
			List<MPagelimit> nodes = (List<MPagelimit>) CacheFactory.getPageLimitCache().getCache();
			if(nodes == null || nodes.size() == 0){
				super.response(response, "1");
			}else{
				Gson gson = new Gson();
				super.response(response, gson.toJson(nodes));
			}
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		}
		
	}
	
	public static void main(String[] args) {
		SPagelimitServlet s = new SPagelimitServlet();
		s.getAllPageNodes(null, null);
	}
	
	/**
	 * 添加权限信息
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("static-access")
	private void addPagelimit(HttpServletRequest request, HttpServletResponse response){
		try {
			MPagelimit page = (MPagelimit) JsonHelper.getObjectByJsonString(request, MPagelimit.class);
			
			if("".equals(page.getParentId())){
				page.setParentId("0");
				page.setPageName("优乐车权限管理");
			}
			
			page.setAddTime(DateUtil.getNowDate());
			page.setA1("0");
			
			page.setId(this.getIdByParentId(page.getParentId()));
			
			BPagelimit handler = new BPagelimit();
			
			if(this.isExistRootNode()){
				super.response(response, "3");
				return;
			}
			
			if(handler.isExistPageByPageName(page.getPageName())){
				super.response(response, "2");
				return;
			}
			
			if(handler.addPageLimit(page) >0 ){
				CacheFactory.getPageLimitCache().updateCache();
				super.response(response, "1");
			}else{
				super.response(response, "0");
			}
		} catch (Exception ex){
			ex.printStackTrace();
			super.response(response, "0");
		}
	}
	
	/**
	 * 判断是否存在根节点
	 * @return 是否存在根节点 true-存在
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	private boolean isExistRootNode(){
		List<MPagelimit> nodes = null;
		try {
			nodes = (List<MPagelimit>) CacheFactory.getPageLimitCache().getCache();
		} catch (Exception e) {
			return false;
		}
		if( nodes !=null && nodes.size() > 0){
			for (MPagelimit node : nodes) {
				if("root".equals(node.getParentId())){
					return true;
				}
			}
		}
		
		return false;
	}

	
	
	/**
	 * 判断是否存在本节点
	 * @return 是否存在根节点 true-存在
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	private boolean isExistNode(String nodeId){
		List<MPagelimit> nodes = null;
		try {
			nodes = (List<MPagelimit>) CacheFactory.getPageLimitCache().getCache();
		} catch (Exception e) {
			return false;
		}
		if( nodes !=null && nodes.size() > 0){
			for (MPagelimit node : nodes) {
				if(nodeId.equals(node.getId())){
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 根据父ID的到新增节点的Id
	 * @param parentId
	 * @return 返回Id
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	private String getIdByParentId(String parentId){
		String id = "";
		List<MPagelimit> nodes = null;
		try {
			nodes = (List<MPagelimit>) CacheFactory.getPageLimitCache().getCache();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(nodes == null || nodes.size() == 0){
			id = "1";
		}else{
			int maxId = 1;
			boolean isExistSublingNode = false;
			for (MPagelimit node : nodes) {
				if(parentId.equals(node.getParentId())){
					if(Integer.parseInt(node.getId()) > maxId){
						isExistSublingNode = true;
						maxId = Integer.parseInt(node.getId());
					}
				}
			}
			
			if(!isExistSublingNode && !parentId.equals("0")){
				id = parentId + "1";
			}else{
				id = ((Integer) (maxId + 1)).toString();
			}
		}
		
		return id;
	}
	
	
}
